package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.MenuDao;
import com.lagou.domain.Menu;
import com.lagou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageInfo<Menu> findAllMenu(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Menu> list = menuDao.findAllMenu();
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Menu findMenuById(int id) {
        Menu menu = menuDao.findMenuById(id);
        return menu;
    }

    @Override
    public List<Menu> findSubMenuListByPid(int pid) {
        List<Menu> list = menuDao.findSubMenuListByPid(pid);
        return list;
    }

    @Override
    public void saveMenu(Menu menu) {
        Date date = new Date();
        menu.setCreatedTime(date);
        menu.setUpdatedTime(date);
        menuDao.saveMenu(menu);
    }

    @Override
    public void updateMenu(Menu menu) {
        menu.setUpdatedTime(new Date());
        menuDao.updateMenu(menu);
    }
}
